package com.eachonline.smart.service.imp;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eachonline.mapper.GenericMapper;
import com.eachonline.service.GenericServiceImpl;
import com.eachonline.smart.mapper.SmartClassTeacherMapper;
import com.eachonline.smart.mapper.SmartCourseMapper;
import com.eachonline.smart.mapper.SmartUserClassMapper;
import com.eachonline.smart.model.SmartClassTeacher;
import com.eachonline.smart.model.SmartClassTeacherExample;
import com.eachonline.smart.model.SmartCourse;
import com.eachonline.smart.model.SmartUserClass;
import com.eachonline.smart.service.ClassTeacherService;
import com.eachonline.util.UUIDUtil;
@Service
public class ClassTeacherServiceImp extends GenericServiceImpl<SmartClassTeacher, SmartClassTeacherExample, String> implements ClassTeacherService {

	@Autowired
	private SmartClassTeacherMapper  classTeacherMapper;
	

	@Autowired
	private SmartCourseMapper  courseMapper;
	
	
	@Autowired
	private SmartUserClassMapper userClassMapper;

	@Override
	protected GenericMapper<SmartClassTeacher, SmartClassTeacherExample, String> getGenericMapper() {
		// TODO Auto-generated method stub
		return this.classTeacherMapper;
	}

	
	@Override
	public void createClassByCourse(String courseId) throws Exception {
		// TODO Auto-generated method stub
		
	 SmartCourse course= courseMapper.selectByPrimaryKey(courseId);
	 if(course==null)
	 {
		 throw new Exception("课程不存在");
	 }
	 
	 
	 SmartClassTeacherExample example=new SmartClassTeacherExample();
	 example.createCriteria().andCourseIdEqualTo(courseId);
	 List<SmartClassTeacher>  classesList=classTeacherMapper.selectByExample(example);
	 if(classesList.isEmpty())
	 {
		 
		 SmartClassTeacher  smartClass=new SmartClassTeacher();
		 smartClass.setSid(UUIDUtil.getUUID());
		 smartClass.setClassName(course.getCourseName());
		 smartClass.setClassPic(course.getMainCoursePic());
		 smartClass.setClassTeacher(course.getTeacherName());
		 smartClass.setComment(course.getCourseIntro());
		 smartClass.setCourseNum(course.getCourseNum());
		 smartClass.setCreateTime(new Date());
		 smartClass.setCourseId(courseId);
		 smartClass.setStudentNum(0);
		 smartClass.setUserId(course.getUserSid());
		 smartClass.setClassNum(course.getStudentNum());
		 
		 classTeacherMapper.insert(smartClass);
	 }
	
		
	}


	@Override
	public void joinClass(String courseId, String userId) throws Exception {
		// TODO Auto-generated method stub
		
		 SmartCourse course= courseMapper.selectByPrimaryKey(courseId);
		 if(course==null)
		 {
			 throw new Exception("课程不存在");
		 }
		 
		 SmartClassTeacherExample example=new SmartClassTeacherExample();
		 example.createCriteria().andCourseIdEqualTo(courseId);
		 List<SmartClassTeacher>  classesList=classTeacherMapper.selectByExample(example);
		 if(classesList.isEmpty())
		 {
			 throw new Exception("课程对应的班级不存在");
		 }
		 SmartClassTeacher classes=classesList.get(0);
		 
		 SmartUserClass  userClass=new SmartUserClass();
		 userClass.setSid(UUIDUtil.getUUID());
		 userClass.setCreateTime(new Date());
		 userClass.setUserId(userId);
		 userClass.setClassId(classes.getSid());
		 userClassMapper.insert(userClass);
		 
		 classes.setStudentNum(classes.getStudentNum()+1);
		 
		 classTeacherMapper.updateByPrimaryKeySelective(classes);
		
	}

	
	
}